/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2011-2012 www.pretty-tools.com
 */

var wwwPrettyTools_VKontakteButtonConfiguration = {
    BRANCH: "extensions.prettytools.button.vkontakte.",
    INSTALLED_PARAM: "installed.v1_1",
    SHOW_ZEROS_PARAM: "show_zeros",
    POPUP_PARAM: "popup",
    VOTES_PARAM: "votes"
};

wwwPrettyTools_VKontakteButtonConfiguration.getBranch = function ()
{
    return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).QueryInterface(Components.interfaces.nsIPrefBranch).getBranch(this.BRANCH);
}

wwwPrettyTools_VKontakteButtonConfiguration.isFirstStart = function ()
{
    try
    {
        var branch = this.getBranch();
        if (branch.prefHasUserValue(this.INSTALLED_PARAM))
            return false;

        branch.setBoolPref(this.INSTALLED_PARAM, true);
        return true;
    }
    catch(e)
    {
        dump("Exception during isFirstStart: " + ex + "\n");
        return false;
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.isOpenInPopup = function ()
{
    try
    {
        var branch = this.getBranch();

        if (!branch.prefHasUserValue(this.POPUP_PARAM))
            return true;// Open in popup by default

        return branch.getBoolPref(this.POPUP_PARAM);
    }
    catch(e)
    {
        dump("Exception during isOpenInPopup: " + ex + "\n");
        return false;
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.setOpenInPopup = function (bOpenInPopup)
{
    try
    {
        this.getBranch().setBoolPref(this.POPUP_PARAM, bOpenInPopup);
    }
    catch(e)
    {
        dump("Exception during setOpenInPopup: " + ex + "\n");
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.getShowVotes = function ()
{
    try
    {
        var branch = this.getBranch();

        if (!branch.prefHasUserValue(this.VOTES_PARAM))
            return 1;// Show if possible

        return branch.getIntPref(this.VOTES_PARAM);
    }
    catch(e)
    {
        dump("Exception during getShowVotes: " + ex + "\n");
        return 1;
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.setShowVotes = function (nShowVotes)
{
    try
    {
        this.getBranch().setIntPref(this.VOTES_PARAM, nShowVotes);
    }
    catch(e)
    {
        dump("Exception during setShowVotes: " + ex + "\n");
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.isShowZeros = function ()
{
    try
    {
        var branch = this.getBranch();

        if (!branch.prefHasUserValue(this.SHOW_ZEROS_PARAM))
            return false;// Do not show zeros by default

        return branch.getBoolPref(this.SHOW_ZEROS_PARAM);
    }
    catch(e)
    {
        dump("Exception during isShowZeros: " + ex + "\n");
        return false;
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.setShowZeros = function (bShowZeros)
{
    try
    {
        this.getBranch().setBoolPref(this.SHOW_ZEROS_PARAM, bShowZeros);
    }
    catch(e)
    {
        dump("Exception during setShowZeros: " + ex + "\n");
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.onOptionsPageLoad = function ()
{
    try
    {
        document.getElementById("openIn").selectedIndex = this.isOpenInPopup() ? 1 : 0;
        document.getElementById("showVotes").selectedIndex = this.getShowVotes();
    }
    catch(e)
    {
        dump("Exception during onOptionsPageLoad: " + ex + "\n");
    }
}

wwwPrettyTools_VKontakteButtonConfiguration.onOptionsOK = function ()
{
    try
    {
        this.setOpenInPopup(document.getElementById("popup").selected);
        this.setShowVotes(document.getElementById("showVotes").selectedIndex);
    }
    catch(e)
    {
        dump("Exception during onOptionsOK: " + ex + "\n");
    }
}
